package br.rui.iof.visao.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.rui.iof.negocio.Fachada;

/**
 * Servlet implementation class for Servlet: RedirecionarServlet
 *
 * @web.servlet
 *   name="RedirecionarServlet"
 *   display-name="RedirecionarServlet" 
 *
 * @web.servlet-mapping
 *   url-pattern="/RedirecionarServlet"
 *  
 */
 @SuppressWarnings("serial")
public class RedirecionarServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}  	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String acao = request.getParameter("acao");
		String url = "";		
		if(acao!=null){
			try {

				if(acao.equalsIgnoreCase("cadastrarContaIOF")){
					setRequestClientes(request);
					url = "index.jsp?acao=iof&interna=iof_"+acao;
				}
				if(acao.equalsIgnoreCase("cadastrarLancamentoIOF")){
					setSessionClientes(request);
					url = "index.jsp?acao=iof&interna=iof_"+acao;
				}
				if(acao.equalsIgnoreCase("consultarContasIOF")){
					setRequestClientes(request);
					url = "index.jsp?acao=iof&interna=iof_"+acao;
				}
				if(acao.equalsIgnoreCase("calcularIOF")){
					setSessionClientes(request);
					url = "index.jsp?acao=iof&interna=iof_"+acao;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		request.getRequestDispatcher(url).forward(request,response);
	}
	
	private void setRequestClientes(HttpServletRequest request){
		request.setAttribute("clientes", Fachada.recuperarClientes());
	}
	private void setSessionClientes(HttpServletRequest request){
		HttpSession session = request.getSession(false);
		session.setAttribute("clientes", Fachada.recuperarClientes());
	}
}